using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Model;
using ZR.Model.Dto;
using ZR.Model.Business;
using ZR.Repository;
using ZR.Service.Business.IBusinessService;
using System.Linq;

namespace ZR.Service.Business
{
    /// <summary>
    /// 产品管理Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ISalesService), ServiceLifetime = LifeTime.Transient)]
    public class SalesService : BaseService<Sales>, ISalesService
    {
        /// <summary>
        /// 查询产品管理列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<SalesDto> GetList(SalesQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                //.OrderBy("Id asc")
                .Where(predicate.ToExpression())
                .ToPage<Sales, SalesDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Sales GetInfo(int Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加产品管理
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Sales AddSales(Sales model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改产品管理
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateSales(Sales model)
        {
            return Update(model, true);
        }



        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<Sales> QueryExp(SalesQueryDto parm)
        {
            var predicate = Expressionable.Create<Sales>();

            predicate = predicate.AndIF(parm.ModelId != null, it => it.ModelId == parm.ModelId);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.Ipaddress), it => it.Ipaddress.Contains(parm.Ipaddress));
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.Localtion), it => it.Localtion.Contains(parm.Localtion));
            return predicate;
        }
    }
}